package com.tms.app.tms.ui.base.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jph.takephoto.model.TResult;
import com.tms.app.tms.R;
import com.tms.app.tms.bean.UserInfo;
import com.tms.app.tms.bean.response.UserInfoRes;
import com.tms.app.tms.ui.ShowPhotoActivity;
import com.tms.app.tms.ui.home.me.UserInfoPresenter;
import com.tms.app.tms.utils.PhotoHelper;
import com.tms.app.tms.widget.MyDialogPhoto;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.zafu.camera.activity.CameraIdCradActivity;

/**
 * Created by Administrator on 2017/11/22 0022.
 */

public abstract class AbsPhotoPickWithPs extends BasePhotoActivity implements View.OnLongClickListener {
    protected ArrayList<View> imgLayoutList = new ArrayList<>();
    private List<Integer> imgResList=new ArrayList<>();
    protected ArrayList<ImageView> imgViewList = new ArrayList<>();
    protected ArrayList<ImageView> imgViewList2 = new ArrayList<>();
    protected ArrayList<TextView> btnDelList = new ArrayList<>();
    protected Map<ImageView,String> selectImgList =new HashMap<>();//选取的图片路径
    protected Map<ImageView,String> serverImgMap =new HashMap<>();//选取的图片路径
    public MyDialogPhoto myDialogPhoto;
    public  ImageView indexView;
    private int index=0;
    protected UserInfoPresenter presenter;
    protected UserInfo userInfo;
    private  int isResImg=1;

    public List<Integer> getImgResList() {
        return imgResList;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imgViewList.clear();
        btnDelList.clear();
        imgResList=intitImgBg();
        initLayout();
        initData();
    }

    public abstract void initLayout();

    public abstract List<Integer> intitImgBg();

    protected void initData() {
        if (imgViewList.size() > 0)
            return;

        imgLayoutList.add(findViewById(R.id.img_parent_1));//FrameLayout
        imgLayoutList.add(findViewById(R.id.img_parent_2));
        imgLayoutList.add(findViewById(R.id.img_parent_3));
        imgLayoutList.add(findViewById(R.id.img_parent_4));

        imgViewList.add((ImageView) findViewById(R.id.img_1_1));// 示例图层
        imgViewList.add((ImageView) findViewById(R.id.img_2_1));
        imgViewList.add((ImageView) findViewById(R.id.img_3_1));
        imgViewList.add((ImageView) findViewById(R.id.img_4_1));

        imgViewList2.add((ImageView) findViewById(R.id.img_1_2));// 选择的图
        imgViewList2.add((ImageView) findViewById(R.id.img_2_2));
        imgViewList2.add((ImageView) findViewById(R.id.img_3_2));
        imgViewList2.add((ImageView) findViewById(R.id.img_4_2));

        btnDelList.add((TextView) findViewById(R.id.photo_button_del_1));// 删除的图层
        btnDelList.add((TextView) findViewById(R.id.photo_button_del_2));
        btnDelList.add((TextView) findViewById(R.id.photo_button_del_3));
        btnDelList.add((TextView) findViewById(R.id.photo_button_del_4));


        for (TextView textView : btnDelList) {
            textView.setOnClickListener(this);
        }

        for (ImageView imageView : imgViewList) {
            imageView.setOnClickListener(this);
        }
        for (ImageView imageView : imgViewList2) {
            imageView.setOnClickListener(this);
           imageView.setOnLongClickListener(this);
            selectImgList.put(imageView,null);
        }
        if (imgResList !=null&& imgResList.size()>0){// 示例图集合
            int size=imgResList.size()>imgViewList.size()?imgViewList.size():imgResList.size();
            for (int i = 0; i < size; i++) {
                imgViewList.get(i).setBackgroundResource(imgResList.get(i));
                imgViewList.get(i).setVisibility(View.VISIBLE);
            }
        }

    }

    public void showServerImg(String ... img) {
        int lenghta=img.length>imgViewList2.size()?imgViewList2.size():img.length;
        for (int i = 0; i < lenghta; i++) {
            if (img[i]!=null&&img[i].length()>0){
                Glide.with(this).load(img[i]).into(imgViewList2.get(i));
                imgViewList2.get(i).setVisibility(View.VISIBLE);
                serverImgMap.put(imgViewList2.get(i),img[i]);
            }

        }
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        if ((System.currentTimeMillis() - mExitTime) < 2000) {
            mExitTime = System.currentTimeMillis();
            return;
        }
        Intent intent=null;
        switch (v.getId()) {
            case R.id.img_1_2:
            case R.id.img_2_2:
            case R.id.img_3_2:
            case R.id.img_4_2:
                imgSelectClick(v);
                break;
          /*  图片事件结束*/
            case R.id.dialog_photo_text_top_1:
                intent = new Intent(this, CameraIdCradActivity.class);
                intent.putExtra("path", "/sdcard/tms/img/");
                intent.putExtra("name", System.currentTimeMillis()+".jpg");
                intent.putExtra("type", "idcardFront");
                startActivityForResult(intent,100);
                if (myDialogPhoto != null)
                    myDialogPhoto.close();
                break;
            case R.id.dialog_photo_text_top_2:
                PhotoHelper.pickPhoto(getTakePhoto());
                if (myDialogPhoto != null)
                    myDialogPhoto.close();
                break;
            case R.id.dialog_photo_text_top_3:
                if (isResImg==1){
                    intent=new Intent(this,ShowPhotoActivity.class);
                    intent.putExtra("imgPath" , imgResList.get(index));
                    intent.putExtra("imgType",1);
                    startActivity(intent);
                    if (myDialogPhoto != null)
                        myDialogPhoto.close();
                    }
                else {
                    intent=new Intent(this,ShowPhotoActivity.class);
                    if (selectImgList.get(indexView)!=null&&!selectImgList.get(indexView).equals("-1"))
                    {
                        intent.putExtra("imgPath", selectImgList.get(indexView));
                    }
                    else
                    {
                        intent.putExtra("imgPath", serverImgMap.get(indexView));
                    }

                    intent.putExtra("imgType",0);
                    startActivity(intent);
            }
                break;
            case R.id.dialog_photo_button_1:
                break;
            case R.id.dialog_photo_button_2:
                break;
            case R.id.dialog_photo_button_3:
                break;
            case R.id.img_1_1:
            case R.id.img_2_1:
            case R.id.img_3_1:
            case R.id.img_4_1:
                imgClick(v);
                break;
        }

    }

    private void imgSelectClick(View v){
        switch (v.getId()) {
            case R.id.img_1_2:
                indexView= (ImageView) findViewById( R.id.img_1_2);
                break;
            case R.id.img_2_2:
                indexView= (ImageView) findViewById( R.id.img_2_2);
                break;
            case R.id.img_3_2:
                indexView= (ImageView) findViewById( R.id.img_3_2);
                break;
            case R.id.img_4_2:
                indexView= (ImageView) findViewById( R.id.img_4_2);
                break;


        }
        if (myDialogPhoto!=null&&myDialogPhoto.isShowing()){
            return;
        }
        myDialogPhoto=new MyDialogPhoto(this, this, 0);
        myDialogPhoto.show();
        isResImg=0;
    }

   /* 示例图片事件*/
    private void imgClick(View view){
        switch (view.getId()){
            case R.id.img_1_1:
                if (myDialogPhoto!=null&&myDialogPhoto.isShowing()){
                    return;
                }
                myDialogPhoto=new MyDialogPhoto(this, this, 1);
                myDialogPhoto.show();
                indexView= (ImageView) findViewById( R.id.img_1_2);
                index=0;
                break;
            case R.id.img_2_1:
                if (myDialogPhoto!=null&&myDialogPhoto.isShowing()){
                    return;
                }
                myDialogPhoto=new MyDialogPhoto(this, this, 1);
                myDialogPhoto.show();
                indexView= (ImageView) findViewById( R.id.img_2_2);
                index=1;
                break;
            case R.id.img_3_1:
                if (myDialogPhoto!=null&&myDialogPhoto.isShowing()){
                    return;
                }
                myDialogPhoto=new MyDialogPhoto(this, this, 1);
                myDialogPhoto.show();
                indexView= (ImageView) findViewById(R.id.img_3_2);
                index=2;
                break;
            case R.id.img_4_1:
                if (myDialogPhoto!=null&&myDialogPhoto.isShowing()){
                    return;
                }
                myDialogPhoto=new MyDialogPhoto(this, this, 1);
                myDialogPhoto.show();
                indexView= (ImageView) findViewById(R.id.img_4_2);
                index=3;
                break;
        }
        isResImg=1;
    }

    @Override
    public void takeSuccess(final TResult result) {
        super.takeSuccess(result);
        if (result.getImage() == null)
            return;
        selectImgList.put(indexView,result.getImage().getCompressPath());
        Glide.with(this).load(result.getImage().getCompressPath()).into(indexView);
        indexView.setVisibility(View.VISIBLE);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            if (resultCode == RESULT_OK) {
                Bundle extras = data.getExtras();
                String path = extras.getString("path");
                Glide.with(this).load(new File(path)).into(indexView);
                indexView.setVisibility(View.VISIBLE);
                selectImgList.put(indexView,path);
            }
        }
    }

    @Override
    public boolean onLongClick(View v) {
        v.setVisibility(View.GONE);
        selectImgList.put(indexView,null);
        return  false;
    }
}





